#ifndef MD5SUM_HPP
#define MD5SUM_HPP

#include <iostream>
#include <openssl/md5.h>
class Md5Sum
{

public:
    static std::string calculate(const std::string &input)
    {
        unsigned char hash[MD5_DIGEST_LENGTH];
        // hash中存储的是二进制数据
        MD5(reinterpret_cast<const unsigned char *>(input.c_str()), input.size(), hash);
        std::string res;
        for (int i = 0; i < MD5_DIGEST_LENGTH; ++i)
        {
            int value = static_cast<int>(hash[i]);
            std::string hex = "";
            while (value > 0)
            {
                int temp = value % 16;
                if (temp < 10)
                    hex = static_cast<char>(temp + '0') + hex;
                else
                    hex = static_cast<char>(temp - 10 + 'a') + hex;
                value /= 16;
            }
            if (hex.size() == 1)
                hex = "0" + hex;

            res += hex;
        }
        return res;
    }
};
#endif